package com.itheima.day05.shop.service.impl;


import com.itheima.day05.shop.dao.ProductRepository;
import com.itheima.day05.shop.pojo.Product;
import com.itheima.day05.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


public class ProductServiceImpl implements ProductService {

    private ProductRepository productRepository;

    public List<Product> getAllProducts() {
        return productRepository.findAll();
    }

    public Product getProductById(Long id) {
        return productRepository.findById(id);
    }

    public void saveProduct(Product product) {
         productRepository.save(product);
    }

    public void deleteProduct(Long id) {
        productRepository.deleteById(id);
    }

    public List<Product> getProductsByPriceRange(double minPrice, double maxPrice) {
        List<Product> allProducts = productRepository.findAll();
        List<Product> list = allProducts.stream().filter(product -> product != null && (product.getPrice() >= minPrice && product.getPrice() <= maxPrice)).collect(Collectors.toList());
        return list;
    }

    @Override
    public void updateProduct(Product product) {
        productRepository.save(product);
    }
}